package de.hpi.generator.config;


import java.util.ArrayList;

public class TableModel {
	/**
	 * @uml.property  name="name"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="de.hpi.generator.model.Column"
	 */
	public String name;
	/**
	 * @uml.property  name="rowNumbers"
	 */
	public int rowNumbers = 0;
	/**
	 * @uml.property  name="columns"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="de.hpi.generator.config.ColumnModel"
	 */
	public ArrayList<ColumnModel> columns = new ArrayList<ColumnModel>();
	public DependencyModels FunctionalDependencies = null;
	
	public TableModel(){
		
	}
	
	public TableModel(String aName, int aRowNumbers) {
		this.name  = aName;
		this.rowNumbers  = aRowNumbers;
	}
	
	public TableModel(String aName, int aRowNumbers, DependencyModels FunctionalDependencies) {
		this.name  = aName;
		this.rowNumbers  = aRowNumbers;
		this.FunctionalDependencies = FunctionalDependencies;
	}

	public void addColumnModel(ColumnModel aColumnModel) throws ExistsException {
		if (findColumnModelByName(aColumnModel.name) == null) {
			this.columns.add(aColumnModel);
		} else {
			throw new ExistsException("The table " + aColumnModel.name + " is already exists");
		}
	}
	
	public void addFunctionalDependencyModel(DependencyModel aFunctionalDependencyModel) throws ExistsException {
		if(this.FunctionalDependencies==null)
			this.FunctionalDependencies= new DependencyModels();
		this.FunctionalDependencies.add(aFunctionalDependencyModel);

	}
	
	
	@Override
	public String toString(){
		String result =  String.format("{name: %s, rowNumbers: %d \n \tcolumns:[ \n", this.name, this.rowNumbers);
		String aColumnModelsString = "";
		for (ColumnModel aColumnModel : columns) {
			aColumnModelsString+="\t\t" + aColumnModel.toString();
		}
		return result + aColumnModelsString + "\t]}\n";
	}


	public ColumnModel findColumnModelByName(String aColumnModelName) {
		ColumnModel result = null;
		for (ColumnModel aTestObject : this.columns) {
			if(aTestObject.name.compareTo(aColumnModelName) == 0) {
				 result = aTestObject;
			}
		}
		return result;
	}

}
